Generator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 9
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A generate 0 3 1
A constructor 0 3 1
1
2
function pickMap(map) {
3
    const rand = Math.random();
4
5
    let accum = 0;
6
7
    for (const token of Object.keys(map)) {
8
        const curr = map[token];
9
10
        accum += curr;
11
        if (rand < accum) return token;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
12
    }
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
13
}
14
15
export default class Generator {
16
    constructor(model) {
17
        this._model = model.matrix;
18
    }
19
20
    generate() {
21
        return pickMap(this._model);
22
    }
23
}
24